# -*- coding: utf-8 -*-

from flask import request
from flask import render_template
from . import api_users
from . import api_article
from . import handler_article
from . import handler_usr


@api_users.route('/genincode',methods=['POST'])
def genincode():
    data = request.form
    return handler_usr.gencode(data['username'],data['password'])


@api_users.route('/signin', methods=['POST'])
def index():
    data = request.form
    res = handler_usr.VerifyUser(data['username'], data['password'])
    if res == 0:
        return render_template('admin/main.html')
    else:
        if res == 1:
            return 'pwdfail'
        else:
            return 'userfail'

@api_users.route('/register', methods=['POST'])
def register():
    data = request.form
    res = handler_usr.UserRegister(data['username'],data['password'],data['incode'])
    if res == 0:
        return 'success'
    else:
        if res == 2:
            return 'namefail'
        else:
            return 'codefail'


@api_article.route('/submitmd', methods=['POST'])
def submit():
    data = request.form
    return str(handler_article.SubmitData(data, 'content_md'))


@api_article.route('/submithtml', methods=['POST'])
def submithtml():
    data = request.form
    return str(handler_article.SubmitData(data, 'content_html'))

@api_article.route('/submitMinder/<types>', methods=['POST'])
def submitminderX(types):
    data = request.form
    return str(handler_article.SubmitMinder(data['content'], types))

# data get

@api_article.route('/art/<types>', methods=['POST'])
def webArticle(types):
    sheet = types + '_article'
    htmlx = handler_article.genHtml(sheet)
    data = request.form
    if str(data['loc']) == 'title':
        return htmlx[0]
    else:
        handler_article.deHtml(sheet)
        return htmlx[1]

@api_article.route('/test')
def test():
    return render_template('admin/minder.html')

